package com.zhaoc.service.impl;

import com.zhaoc.dao.RoleRepository;
import com.zhaoc.dao.UserRepository;
import com.zhaoc.domain.system.Role;
import com.zhaoc.domain.system.User;
import com.zhaoc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/21 0021
 * time:11:03
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    //根据用户查询用户的角色
    @Override
    public List<Role> findByUser(User user) {
        return null;
    }

    //查询所有用户
    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }


    //保存用户
    @Override
    public void save(User user, String[] roleTd) {
        //调用dao层保存用户
        userRepository.save(user);
        //用户关联角色
        if (roleTd != null){
            for (String roleId : roleTd) {
                Role role = roleRepository.findOne(Integer.parseInt(roleId));
                user.getRoles().add(role);
            }

        }
    }
}
